<template>
	<div class="test2">
		<div class="u_resourceH classH clear">
			<div class="u_resourceH_l fl">
				<template v-if="!$store.state.isSchool">
					<el-select filterable placeholder="请选择" v-model="subjectId" @change="changeSelect">
						<el-option v-for="(item, index) in subjectList" :key="index" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</template>
			</div>
			<!--  -->
			<div class="u_resourceH_r fr">
				<el-input size="medium" placeholder="试题名称" suffix-icon="el-icon-search" @change="changSearch" v-model="keyword">
				</el-input>
			</div>
		</div>
		<!--  -->
		<div class="classBox class_test" v-loading="loading">
			<ul>
				<li class="testLs clear" v-for="(item, index) in questionList" :key="item.id">
					<!-- top -->
					<div class="i_choiceT clear">
						<div class="i_choiceT_l fl clear">
							<span>{{item.type_name}}</span>
							<span>难度 : {{item.difficulty_name}}</span>
						</div>
						<div class="i_choiceT_r fr">
							<span class="bcxc" @click="getCancel(item.id)">取消收藏</span>
							<el-popover placement="bottom-end" width="400" popper-class="analyP" trigger="click">
								<span slot="reference" class="bcxc">查看解析</span>
								<div class="i_analysisBox">
									<ul>
										<li class="i_analysis clear" v-if="item.answer">
											<span>答案</span>
											<div>
												<span v-for="val in item.answer">{{val}}</span>
											</div>
										</li>
										<li class="i_analysis clear">
											<span>统计</span>
											<div>
												本题共被作答{{item.question_info.answer_count}}次，正确率为{{item.question_info.true_probability}}%
											</div>
										</li>
										<li class="i_analysis clear">
											<span>解析</span>
											<template v-if="item.pattern == 1">
												<div v-html="item.resolve"></div>
											</template>
											<template v-if="item.pattern == 2">
												<div>
													<img :src="BASE_URL + item.resolve_image_path">
												</div>
											</template>
										</li>
									</ul>
								</div>
							</el-popover>
						</div>
					</div>
					<!-- cont -->
					<div class="i_choiceC">
						<template v-if="item.pattern === 1">
							<div class="i_testTxt i_word" v-html="item.content"></div>
						</template>
						<template v-if="item.pattern === 2">
							<div class="i_testTxt i_word">
								<img :src="BASE_URL + item.content_image_path">
							</div>
						</template>
						<ul>
							<li class="i_choiceLs i_word clear" v-for="(item2, index2) in item.option" :key="index2">
								<label>
									<input type="radio" name="1" disabled="">
									{{item2.name}}<i v-if="item2.value">.</i>
								</label>
								<div class="i_choiceLs_c" v-html="item2.value"></div>
							</li>
						</ul>
					</div>
				</li>
			</ul>
		</div>
		<div class="pageBox" v-if="total_number > page_size">
			<el-pagination @current-change="change_page" :page-size="page_size" :current-page="page_index" layout="prev, pager, next" :total="total_number">
			</el-pagination>
		</div>
		<div class="dependent" v-if="placeholder">
			<i class="ipm image_nofound"></i>
			<p>
				还没发现任何相关的内容哦~
			</p>
		</div>
	</div>
</template>
<script>
export default {
	name: '',
	data() {
		return {
			subjectId: 0, //学科
			subjectList: this.$g.subjectList, //学科表
			keyword: '', //资源名称

			questionList: [],
			page_index: 1,
			page_size: this.$g.page_size,
			total_number: 0,
			placeholder: false,
			loading: true
		}
	},
	created() {
		this.getQuestion();
	},
	methods: {
		getQuestion() {
			let success = (res => {
				let data = res.data;

				this.questionList = data;
				this.total_number = data.data_sum;
				this.page_index = parseInt(data.page_index);
				this.subjectList = this.$g.subjectList
				this.questionList.forEach(item => {
					if (item.option) {
						item.option = JSON.parse(item.option)
					}

					if (item.answer) {
						item.answer = JSON.parse(item.answer)
					}
				})

				this.loading = false
				this.placeholder = this.questionList.length === 0 ? true : false
			});

			this.$ajax({
				url: '/api/favourite/favourite',
				method: 'get',
				params: {
					origin: 2,
					subject_id: this.subjectId,
					keyword: this.keyword,
					user_id: this.$g.user_id,
					page_index: this.page_index,
					page_size: this.page_size,
				},
				func: { success: success }
			})
		},
		getCancel(res_id) {
			let success = (res => {
				this.$message.success(res.msg)
				this.getQuestion()
			})
			this.$confirm('此操作是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				customClass: 'confirm-them',
			}).then(() => {
				let success = (res => {
					this.$message.success(res.msg)
					this.getQuestion()
				})
				this.$ajax({
					url: '/api/favourite/favourite',
					method: 'delete',
					params: {
						user_id: this.$g.user_id,
						origin: 2,
						res_id: res_id
					},
					func: { success: success }
				})
			}).catch(() => {});
		},

		//搜索
		changSearch() {
			this.page_index = 1
			this.getQuestion();
		},
		//分页
		change_page(val) {
			this.page_index = 1
			this.getQuestion();
		},
		//筛选
		changeSelect(val) {
			this.page_index = 1
			this.getQuestion();
		},
	}
}
</script>